﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System.Windows;
using System.Windows.Interactivity;
using Tww.Wpf.Controls.Controls.Glows;

namespace Tww.Wpf.Controls.Controls.Windows
{
    public class GlowWindowBehavior : Behavior<Window>
    {
        #region  - Felder (privat) -

        private GlowWindow bottom;
        private GlowWindow left, right, top;

        #endregion

        #region  - Methoden (privat) -

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.Loaded += AssociatedObjectOnLoaded;
        }

        private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            // now glow effect if UseNoneWindowStyle is true or GlowBrush not set
            var metroWindow = AssociatedObject as MetroWindow;
            if (metroWindow != null && (metroWindow.UseNoneWindowStyle || metroWindow.GlowBrush == null))
            {
                return;
            }

            left = new GlowWindow(AssociatedObject, GlowDirection.Left);
            right = new GlowWindow(AssociatedObject, GlowDirection.Right);
            top = new GlowWindow(AssociatedObject, GlowDirection.Top);
            bottom = new GlowWindow(AssociatedObject, GlowDirection.Bottom);

            Show();
            Update();

            bool windowTransitionsEnabled = metroWindow != null && metroWindow.WindowTransitionsEnabled;
            if (!windowTransitionsEnabled)
            {
                // no storyboard so set opacity to 1
                SetOpacityTo(1);
            }
            else
            {
                // start the opacity storyboard 0->1
                StartOpacityStoryboard();
                // hide the glows if window get invisible state
                AssociatedObject.IsVisibleChanged += AssociatedObjectIsVisibleChanged;
                // closing always handled
                AssociatedObject.Closing += (o, args) =>
                {
                    if (!args.Cancel)
                    {
                        AssociatedObject.IsVisibleChanged -= AssociatedObjectIsVisibleChanged;
                    }
                };
            }
        }

        private void AssociatedObjectIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!AssociatedObject.IsVisible)
            {
                // the associated owner got invisible so set opacity to 0 to start the storyboard by 0 for the next visible state
                SetOpacityTo(0);
            }
            else
            {
                StartOpacityStoryboard();
            }
        }

        /// <summary>
        ///     Updates all glow windows (visible, hidden, collapsed)
        /// </summary>
        private void Update()
        {
            if (left != null
                && right != null
                && top != null
                && bottom != null)
            {
                left.Update();
                right.Update();
                top.Update();
                bottom.Update();
            }
        }

        /// <summary>
        ///     Sets the opacity to all glow windows
        /// </summary>
        private void SetOpacityTo(double newOpacity)
        {
            if (left != null
                && right != null
                && top != null
                && bottom != null)
            {
                left.Opacity = newOpacity;
                right.Opacity = newOpacity;
                top.Opacity = newOpacity;
                bottom.Opacity = newOpacity;
            }
        }

        /// <summary>
        ///     Starts the opacity storyboard 0 -> 1
        /// </summary>
        private void StartOpacityStoryboard()
        {
            if (left != null && left.OpacityStoryboard != null
                && right != null && right.OpacityStoryboard != null
                && top != null && top.OpacityStoryboard != null
                && bottom != null && bottom.OpacityStoryboard != null)
            {
                left.BeginStoryboard(left.OpacityStoryboard);
                right.BeginStoryboard(right.OpacityStoryboard);
                top.BeginStoryboard(top.OpacityStoryboard);
                bottom.BeginStoryboard(bottom.OpacityStoryboard);
            }
        }

        /// <summary>
        ///     Shows all glow windows
        /// </summary>
        private void Show()
        {
            left.Show();
            right.Show();
            top.Show();
            bottom.Show();
        }

        #endregion
    }
}